package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Customer;


@Repository
public class CustomerDAO {
	
	@PersistenceContext private EntityManager em;

	/**
	 * Get all customer from db
	 * 
	 * @return all customers from Customer table
	 */
	@Transactional
	public List<Customer> getCustomers() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = null;

		try {
			query = em.createQuery("SELECT c FROM Customer c", Customer.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return query.getResultList();

		// query.getResultList() equivalent to
		// List<Customer> customers = null;
		// customers = query.getResultList();
	}

	/**
	 * Add customer to customer table
	 * 
	 * @param customer
	 *            Customer entity that contains mapped with each column in
	 *            Customer table
	 * @return
	 */
	@Transactional
	public boolean addCustomer(Customer customer) {
//		EntityManager em = TransactionManager.getEntityManager();

		// Check if customer already in db
		try {
			if (isCustomerExist(customer.getLoginid()) == false) {
				em.persist(customer);
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	private boolean isCustomerExist(String loginid) {
		if (getCustomerById(loginid) != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get a customer by loginid
	 */
	 @Transactional
	private Customer getCustomerById(String loginId) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = null;

		try {
			query = em.createQuery(
					"SELECT c FROM Customer c WHERE loginid = :loginId",
					Customer.class);
			query.setParameter("loginId", loginId);

			List<Customer> customers = query.getResultList();
			if (customers.isEmpty()) {
				return null;
			} else {
				return customers.get(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get a customer by login info (customer id, password)
	 * 
	 * @param custId
	 *            Customer's Id
	 * @param password
	 *            Password
	 * @return A Logged-in Customer
	 */
	 @Transactional
	public Customer getCustomerByLoginInfo(String custId, String password) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = null;

		try {
			query = em
					.createQuery(
							"SELECT c FROM Customer c WHERE loginid = :custId and password = :pw",
							Customer.class);
			query.setParameter("custId", custId);
			query.setParameter("pw", password);

			List<Customer> customers = query.getResultList();
			if (customers.isEmpty()) {
				return null;
			} else {
				return customers.get(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Change customer's password
	 */
	 @Transactional
	public int changeCustomerPassword(String loginid, String newPassword) {
//		EntityManager em = TransactionManager.getEntityManager();
		Query query = null;

		try {
			query = em
					.createQuery("UPDATE Customer c set password = :pw WHERE loginid = :custId");
			query.setParameter("custId", loginid);
			query.setParameter("pw", newPassword);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return query.executeUpdate();
	}

	/**
	 * Update customer's information
	 * 
	 * @param customer
	 * @return
	 */
	 @Transactional
	public int updateCustomerInfo(Customer customer) {
//		EntityManager em = TransactionManager.getEntityManager();
		Query query = null;

		try {
			query = em
					.createQuery("UPDATE Customer c "
							+ "set firstName = :fst_name, lastname = :lst_name, address = :addr, phone = :phone "
							+ "WHERE loginid = :id");
			query.setParameter("id", customer.getLoginid());
			query.setParameter("fst_name", customer.getFirstname());
			query.setParameter("lst_name", customer.getLastname());
			query.setParameter("addr", customer.getAddress());
			query.setParameter("phone", customer.getPhone());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return query.executeUpdate();
	}
}
